home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-24 | 17.8 KB | 715 lines | [TEXT/MPS ] |
- #include <UMacApp.h>
- #include <UPrinting.h>
- #include <UTEView.h>
- #include <Fonts.h>
- #include <ToolUtils.h>
-
- #include "TEDemo.h"
-
- const OSType kSignature = 'JLMT';
- const OSType kFileType = 'JL01';
- const int kWindID = 1001; // This is passed to NewSimpleWindow
- const int kHelpID = 1001; // Help dialog uses DLOG 1001
- const int kPaletteWidth = 640;
- const int kPaletteHeight = 32;
- const int kIconWidth = 32;
- const int kBoxIconID = 256;
- const int kTextIconID = 257;
- const int kBox = 1;
- const int kText = 2;
- const int kPaletteColor = greenColor;
- const int kBoxColor = redColor;
-
- // commands
- const int cHelp = 1001; // for color menu items}
- const int cDrawBox = 2001; // command number for Box sketcher object}
-
- const int cBlack = 5001; // for TextColor menu
- const int cBlue = 5002;
- const int cGreen = 5003;
- const int cRed = 5004;
- const int cWhite = 5005;
-
- int gColorArray[cWhite-cBlack+1];
- Rect gIconRect[kText-kBox+1];
-
- pascal void TTEApplication::ITEApplication(OSType itsMainFileType)
- {
- gColorArray[cBlack-cBlack] = blackColor;
- gColorArray[cBlue-cBlack] = blueColor;
- gColorArray[cGreen-cBlack] = greenColor;
- gColorArray[cRed-cBlack] = redColor;
- gColorArray[cWhite-cBlack] = whiteColor;
- SetRect(&gIconRect[kBox-kBox], 0, 0, kIconWidth, kIconWidth); // left top right bottom
- SetRect(&gIconRect[kText-kBox], kIconWidth, 0, 2 * kIconWidth, kIconWidth);
- IApplication(itsMainFileType);
- RegisterStdType("\pTTextView", 'text');
- if (gDeadStripSuppression)
- {
- TTextView *aTextView;
- aTextView = new TTextView;
- }
- InitPrinting();
- }
-
- pascal struct TDocument *TTEApplication::DoMakeDocument(CmdNumber itsCmdNumber)
- {
-
- TTEDocument* aTEDocument;
- aTEDocument = new TTEDocument;
- FailNIL(aTEDocument);
- aTEDocument->ITEDocument();
- return aTEDocument;
- }
-
- pascal void TTEApplication::PoseModalDialog()
- {
- DialogPtr dPtr;
- short dItem;
-
- dPtr = GetNewDialog(kHelpID, nil, (WindowPtr) -1);
- ModalDialog(nil, &dItem);
- DisposDialog(dPtr);
- }
-
-
- pascal struct TCommand *TTEApplication::DoMenuCommand(CmdNumber aCmdNumber)
- {
- switch (aCmdNumber) {
- case cHelp:
- PoseModalDialog();
- return gNoChanges;
- break;
- default: return inherited::DoMenuCommand(aCmdNumber);
- }
- }
-
-
- #ifdef qDebug
- pascal void TTEApplication::IdentifySoftware()
- {
- ProgramReport("\pTEDemo ©J.Langowski/MacTutor June 1990",false);
- inherited::IdentifySoftware();
- }
- #endif
-
- // ---- Document ----
-
- pascal void TTEDocument::ITEDocument()
- {
- TList *aList;
-
- IDocument(kFileType, kSignature, kUsesDataFork,
- !kUsesRsrcFork, !kDataOpen, !kRsrcOpen);
- fSavePrintInfo = true; // save print info in data fork
- fTextHdl = NewPermHandle(0); // allocate block on heap for characters
- FailNIL(fTextHdl);
- fTextColorCmd = cBlack;
- aList = NewList(); // make empty list of Boxs
- fShapeList = aList;
- }
-
- pascal void TTEDocument::AddShape(TBox *aBox)
- {
- fShapeList->InsertFirst(aBox);
- }
-
-
- pascal void TTEDocument::DeleteShape()
- {
- fShapeList->Delete(fShapeList->First());
- }
-
-
- pascal void TTEDocument::DoMakeViews(Boolean forPrinting)
- {
- const Boolean kSquareDots = true;
- const Boolean kFixedSize = true;
-
- TPaletteView *aPaletteView;
- TTextView *aTextView;
- TStdPrintHandler *aStdHandler;
-
- aPaletteView = new TPaletteView;
- FailNIL(aPaletteView);
- aPaletteView->IPaletteView(this);
- fPaletteView = aPaletteView; // save reference to palette in document
-
- aTextView = new TTextView;
- FailNIL(aTextView);
- aTextView->ITextView(this);
- fTextView = aTextView; // save reference to text view in document
- fTextView->StuffText(fTextHdl); // so view uses the same characters
-
- aStdHandler = new TStdPrintHandler; // make text view printable
- FailNIL(aStdHandler);
- aStdHandler->IStdPrintHandler(this, // document
- aTextView, // view to be printed
- !kSquareDots, // BOOLEAN for ImageWriter
- kFixedSize, // BOOLEAN horizontal page size
- !kFixedSize); // BOOLEAN vertical page size
- }
-
- pascal void TTEDocument::DoMakeWindows()
- {
- TWindow *aWindow;
-
- aWindow = NewPaletteWindow(kWindID, kWantHScrollBar, kWantVScrollBar,
- this, fTextView, fPaletteView, kPaletteHeight, kTopPalette);
- aWindow->AdaptToScreen(); // adjust for various size monitors
- aWindow->SimpleStagger(kStdStaggerAmount, kStdStaggerAmount, &gStdStaggerCount);
- }
-
-
- pascal void CalcDiskSpace(TBox *aBox, CalcDiskSpaceStruct *aCalcDiskSpaceStruct)
- {
- aBox->NeedDiskSpace(&(aCalcDiskSpaceStruct->myDataForkBytes));
- }
-
- pascal void TTEDocument::DoNeedDiskSpace(long *dataForkBytes, long *rsrcForkBytes)
- {
- long textBytes,numberOfShapesBytes,cmdNumberBytes;
- CalcDiskSpaceStruct aCalcDiskSpaceStruct;
-
- /* file format:
- TextCmd Number (integer)
- # shapes (integer)
- data for shape #1
- data for shape #2
- etc.
- text
- */
- inherited::DoNeedDiskSpace(dataForkBytes, rsrcForkBytes); // to save print info
- aCalcDiskSpaceStruct.myDataForkBytes = *dataForkBytes;
- cmdNumberBytes = sizeof(int);
- numberOfShapesBytes = sizeof(fTextColorCmd);
- ForEachShapeDo((DoToObject)CalcDiskSpace,&aCalcDiskSpaceStruct);
- textBytes = GetHandleSize(fTextHdl);
- *dataForkBytes = cmdNumberBytes + numberOfShapesBytes +
- aCalcDiskSpaceStruct.myDataForkBytes + textBytes;
- }
-
-
- pascal void TTEDocument::DoRead(short aRefNum,
- Boolean rsrcExists, Boolean forPrinting)
- {
- long textColorCmdNumberBytes;
- int textColorCmdNumber;
-
- long numberOfShapesBytes;
- int numberOfShapes;
-
- TBox *aBox;
- long eof, fPos, textBytes;
-
- /* file format:
- TextCmd Number (integer)
- # shapes (integer)
- data for shape #1
- data for shape #2
- etc.
- text
- */
- inherited::DoRead(aRefNum, rsrcExists, forPrinting); // print info to disk
-
- textColorCmdNumberBytes = sizeof(textColorCmdNumber); // read text color
- FailOSErr(FSRead(aRefNum, &textColorCmdNumberBytes, (Ptr) &textColorCmdNumber));
- fTextColorCmd = textColorCmdNumber;
-
- numberOfShapesBytes = sizeof(numberOfShapes); // read # items
- FailOSErr(FSRead(aRefNum, &numberOfShapesBytes, (Ptr) &numberOfShapes));
- for (int i = 1; i <= numberOfShapes; i++)
- {
- aBox = new TBox; // make new Box object
- FailNIL(aBox);
- aBox->Read(aRefNum); // read and initialize object
- AddShape(aBox); // add to list in doc
- };
-
- FailOSErr(GetFPos(aRefNum, &fPos)); // get size of text in file
- FailOSErr(GetEOF(aRefNum, &eof)); // get size of data in file
- textBytes = eof - fPos;
- SetHandleSize(fTextHdl, textBytes);
- HLock (fTextHdl);
- FailOSErr(FSRead(aRefNum, &textBytes, *fTextHdl)); // read the text
- HUnlock (fTextHdl);
- }
-
-
- pascal void DoToRect(TBox *aBox, DoToRectStruct *aDoToRectStruct)
- {
- aBox->Write(aDoToRectStruct->myRefNum);
- }
-
- pascal void TTEDocument::DoWrite(short aRefNum, Boolean makingCopy)
- {
- long textColorCmdNumberBytes;
- int textColorCmdNumber;
-
- long numberOfShapesBytes;
- int numberOfShapes;
-
- long textBytes;
- DoToRectStruct aDoToRectStruct;
-
- /* file format:
- TextCmd Number (integer)
- # shapes (integer)
- data for shape #1
- data for shape #2
- etc.
- text
- */
-
- inherited::DoWrite(aRefNum, makingCopy); // print info to disk
-
- textColorCmdNumber = fTextColorCmd; // write text color to disk
- textColorCmdNumberBytes = sizeof(textColorCmdNumber);
- FailOSErr(FSWrite(aRefNum, &textColorCmdNumberBytes, (Ptr) &textColorCmdNumber));
-
- numberOfShapes = fShapeList->fSize; // write # items to disk
- numberOfShapesBytes = sizeof(numberOfShapes);
- FailOSErr(FSWrite(aRefNum, &numberOfShapesBytes, (Ptr) &numberOfShapes));
-
- aDoToRectStruct.myRefNum = aRefNum;
- ForEachShapeDo((DoToObject)DoToRect,&aDoToRectStruct); // write each rect to disk
-
- textBytes = GetHandleSize(fTextHdl);
- HLock (fTextHdl);
- FailOSErr(FSWrite(aRefNum, &textBytes, *fTextHdl)); // write the text
- HUnlock (fTextHdl);
- }
-
- pascal void TTEDocument::ForEachShapeDo(pascal void (*DoToItem)(TObject *item,
- void *DoToItem_Staticlink),void *DoToItem_Staticlink)
- { fShapeList->Each(DoToItem,DoToItem_Staticlink); }
-
-
- pascal void DisposeRect(TBox *aBox, void *link)
- { aBox->Free(); }
-
-
- pascal void TTEDocument::Free()
- {
- void *link;
-
- if (fTextHdl != nil) DisposHandle(fTextHdl);
- // dispose of ASCII characters
- ForEachShapeDo((DoToObject)DisposeRect,link);
- // dispose of Box objects
- fShapeList->Free(); // dispose of List object
- inherited::Free(); // dispose of document object
- }
-
-
- #ifdef qDebug
- pascal void TTEDocument::Fields(pascal void (*DoToField) (StringPtr fieldName,
- Ptr fieldAddr, short fieldType, void *link), void *link)
- {
- DoToField("\pTTEDocument", nil, bClass, link);
- DoToField("\pfPaletteView", (Ptr) &fPaletteView, bObject, link);
- DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
- DoToField("\pfTextHdl", (Ptr) &fTextHdl, bHandle, link);
- DoToField("\pfTextColorCmd", (Ptr) &fTextColorCmd, bInteger, link);
- DoToField("\pfShapeList", (Ptr) &fShapeList, bObject, link);
- inherited::Fields(DoToField, link);
- }
- #endif
-
- pascal void TTEDocument::SetTextColorCmd(int theColorCmd)
- { fTextColorCmd = theColorCmd; }
-
-
- pascal int TTEDocument::TextColorCmd()
- { return fTextColorCmd; }
-
-
- //
- // ---- TPaletteView ----
- //
-
- pascal void TPaletteView::IPaletteView(TTEDocument *itsTEDocument)
- {
- VPoint itsSize;
-
- SetVPt(&itsSize, kPaletteWidth, kPaletteHeight);
- IView(itsTEDocument, nil, &gZeroVPt, &itsSize, sizeFixed, sizeFixed);
- fIconSelected = kText-kBox;
- }
-
-
- pascal void TPaletteView::Draw(Rect *area)
- {
- Rect aFrame;
- Point aPenSize;
- Handle aHandle;
-
- ForeColor(kPaletteColor);
-
- aHandle = GetIcon(kBoxIconID);
- FailNILResource(aHandle);
- PlotIcon(&gIconRect[kBox-kBox], aHandle);
-
- aHandle = GetIcon(kTextIconID);
- FailNILResource(aHandle);
- PlotIcon(&gIconRect[kText-kBox], aHandle);
-
- ForeColor(blackColor);
- GetQDExtent(&aFrame);
- SetPt(&aPenSize, 1, 1);
- Adorn(&aFrame, aPenSize, adnLineBottom);
- }
-
- pascal void TPaletteView::DoHighlightSelection(HLState fromHL, HLState toHL)
- {
- Rect aRect;
-
- aRect = gIconRect[fIconSelected];
- InsetRect(&aRect, 1, 1);
- SetHLPenState(fromHL, toHL);
- PaintRect(&aRect);
- }
-
- pascal struct TCommand *TPaletteView::DoMouseCommand(Point *theMouse,
- EventInfo *info, Point *hysteresis)
- {
- int index;
-
- index = int(theMouse->h / kIconWidth);
-
- if ((index < 2) && (index != fIconSelected))
- {
- if (Focus()) { DoHighlightSelection(hlOn, hlOff); };
- fIconSelected = index;
- if (Focus()) { DoHighlightSelection(hlOff, hlOn); };
- };
-
- return gNoChanges;
- }
-
-
- #ifdef qDebug
- pascal void TPaletteView::Fields(pascal void (*DoToField) (StringPtr fieldName,
- Ptr fieldAddr, short fieldType, void *link), void *link)
- {
- DoToField("\pTPaletteView", nil, bClass, link);
- DoToField("\pfIconSelected", (Ptr) &fIconSelected, bInteger, link);
- inherited::Fields(DoToField, link);
- }
- #endif
-
-
- pascal void TTextView::ITextView(TTEDocument *itsTEDocument)
- {
- VPoint itsSize;
- Rect itsInset;
- TextStyle aStyle;
-
- SetVPt(&itsSize, 100, 100);
- SetRect(&itsInset, 10, 8, 10, 0);
- SetTextStyle(&aStyle, applFont, bold, 12, &gRGBBlack);
- ITEView(itsTEDocument, // document
- nil, // superview
- &gZeroVPt, // VPoint - itsLocation = topLeft of view
- &itsSize, // VPoint - width, height of view
- sizePage, // SizeDeterminer - width
- sizeFillPages, // SizeDeterminer - height
- &itsInset, // Rect - inset for TE's viewRect
- &aStyle, // TextStyle
- teJustLeft, // INTEGER - justification
- !kWithStyle, // BOOLEAN - styled TextEdit?
- true); // BOOLEAN - autoWrap?
- fTEDocument = itsTEDocument;
- fPaletteView = itsTEDocument->fPaletteView;
- fUpdated = true;
- }
-
- pascal Boolean TTextView::DoIdle(IdlePhase phase)
- {
- if (!fUpdated)
- {
- if (Focus()) { DrawContents(); fUpdated = true; };
- return inherited::DoIdle(phase);
- }
- }
-
-
- pascal struct TCommand *TTextView::DoKeyCommand(short ch,
- short aKeyCode, EventInfo *info)
- {
- int aQDColor;
-
- aQDColor = gColorArray[fTEDocument->TextColorCmd()-cBlack];
- ForeColor(aQDColor);
- fUpdated = false;
- ForeColor(blackColor);
-
- return inherited::DoKeyCommand(ch, aKeyCode, info);
- }
-
- pascal void TTextView::DoSetupMenus()
- {
- int colorIndex, aColorCmd;
-
- inherited::DoSetupMenus(); // always do this, so other objects get chance
- aColorCmd = fTEDocument->TextColorCmd();
- for (colorIndex = cBlack; colorIndex <= cWhite; colorIndex++)
- EnableCheck(colorIndex, TRUE, (colorIndex == aColorCmd));
- }
-
-
- pascal struct TCommand *TTextView::DoMenuCommand(CmdNumber aCmdNumber)
- {
- TColorCmd *aColorCmd;
-
- switch (aCmdNumber)
- {
- case cBlack:
- case cBlue:
- case cGreen:
- case cRed:
- case cWhite:
- aColorCmd = new TColorCmd;
- FailNIL(aColorCmd);
- aColorCmd->IColorCmd(aCmdNumber, fTEDocument, this);
- return aColorCmd; // return that command object for MacApp
- default: // always do this, so other objects get a chance
- return inherited::DoMenuCommand(aCmdNumber);
-
- }
- }
-
- pascal struct TCommand *TTextView::DoMouseCommand(Point *theMouse,
- EventInfo *info, Point *hysteresis)
- {
- TSketcher *aSketcher;
-
- if (fPaletteView->fIconSelected == kBox-kBox)
- {
- aSketcher = new TSketcher;
- FailNIL(aSketcher);
- aSketcher->ISketcher(fTEDocument, this);
- return aSketcher;
- }
- else
- return inherited::DoMouseCommand(theMouse,info,hysteresis);
- }
-
-
- pascal Boolean TTextView::DoSetCursor(Point localPoint, RgnHandle cursorRgn)
- {
- Rect qdExtent;
-
- GetQDExtent(&qdExtent);
- RectRgn(cursorRgn, &qdExtent);
- if (fPaletteView->fIconSelected == kText-kBox)
- return inherited::DoSetCursor(localPoint, cursorRgn); // use IBeam
- else // for box
- SetCursor(*GetCursor(crossCursor)); // "+"
- return true;
- }
-
-
-
- pascal void DrawYourself(TBox *aBox, void *link)
- {
- aBox->DrawShape();
- }
-
- pascal void TTextView::Draw(Rect *area)
- {
- int aQDColor;
- void *link;
-
- aQDColor = gColorArray[fTEDocument->TextColorCmd()-cBlack];
- ForeColor(aQDColor); // set text color
- inherited::Draw(area); // let TTEView draw the text
- PenNormal();
- ForeColor(kBoxColor); // set box color
- fTEDocument->ForEachShapeDo((DoToObject)DrawYourself,link);
- ForeColor(blackColor);
- }
-
-
- #ifdef qDebug
- pascal void TTextView::Fields(pascal void (*DoToField) (StringPtr fieldName,
- Ptr fieldAddr, short fieldType, void *link), void *link)
- {
- DoToField("\pTTextView", nil, bClass, link);
- DoToField("\pfTEDocument", (Ptr) &fTEDocument, bObject, link);
- DoToField("\pfPaletteView", (Ptr) &fPaletteView, bObject, link);
- DoToField("\pfUpdated", (Ptr) &fUpdated, bBoolean, link);
- inherited::Fields(DoToField, link);
- }
- #endif
-
- pascal void TColorCmd::IColorCmd(int aCmdNumber,
- TTEDocument *itsDocument, TTextView *itsView)
- {
- TScroller *aScroller;
- int oldColorCmd;
-
- fTextView = itsView;
- fTEDocument = itsDocument;
- aScroller = itsView->GetScroller(true);
- ICommand(aCmdNumber, itsDocument, itsView, aScroller);
- oldColorCmd = itsDocument->TextColorCmd(); // menu command number
- fOldColorCmd = oldColorCmd; // menu command number
- fNewColorCmd = aCmdNumber; // menu command number
- }
-
- pascal void TColorCmd::DoIt()
- {
- fTEDocument->SetTextColorCmd(fNewColorCmd);
- fTextView->ForceRedraw();
- }
-
-
- pascal void TColorCmd::RedoIt()
- {
- UndoIt();
- }
-
- pascal void TColorCmd::UndoIt()
- {
- fNewColorCmd = fOldColorCmd;
- fOldColorCmd = fTEDocument->TextColorCmd();
- DoIt();
- }
-
-
- #ifdef qDebug
- pascal void TColorCmd::Fields(pascal void (*DoToField) (StringPtr fieldName,
- Ptr fieldAddr, short fieldType, void *link), void *link)
- {
- DoToField("\pTColorCmd", nil, bClass, link);
- DoToField("\pfTEDocument", (Ptr) &fTEDocument, bObject, link);
- DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
- DoToField("\pfOldColorCmd", (Ptr) &fOldColorCmd, bInteger, link);
- DoToField("\pfNewColorCmd", (Ptr) &fNewColorCmd, bInteger, link);
- inherited::Fields(DoToField, link);
- }
- #endif
-
-
- pascal void TSketcher::ISketcher(TTEDocument *itsDocument, TTextView *itsView)
- {
- TScroller *aScroller;
-
- aScroller = itsView->GetScroller(true);
- ICommand(cDrawBox, itsDocument, itsView, aScroller);
- fTEDocument = itsDocument;
- fTextView = itsView;
- }
-
-
- pascal struct TCommand *TSketcher::TrackMouse(TrackPhase aTrackPhase,
- VPoint *anchorPoint, VPoint *previousPoint,
- VPoint *nextPoint, Boolean mouseDidMove)
- {
- Rect newRect;
- TBox *aBox;
-
- if (aTrackPhase == trackRelease)
- {
- Pt2Rect(fTextView->ViewToQDPt(anchorPoint),
- fTextView->ViewToQDPt(nextPoint), &newRect);
- aBox = new TBox;
- FailNIL(aBox);
- aBox->IBox(&newRect);
- fBox = aBox;
- fBoxLocation = newRect;
- }
- return this;
- }
-
- pascal void TSketcher::DoIt()
- {
- fTEDocument->AddShape(fBox);
- fTextView->InvalidRect(&fBoxLocation);
- }
-
- pascal void TSketcher::RedoIt() { DoIt(); }
-
- pascal void TSketcher::UndoIt()
- {
- fTEDocument->DeleteShape();
- fTextView->InvalidRect(&fBoxLocation);
- }
-
-
- #ifdef qDebug
- pascal void TSketcher::Fields(pascal void (*DoToField) (StringPtr fieldName,
- Ptr fieldAddr, short fieldType, void *link), void *link)
- {
- DoToField("\pTSketcher", nil, bClass, link);
- DoToField("\pfTEDocument", (Ptr) &fTEDocument, bObject, link);
- DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
- DoToField("\pfBox", (Ptr) &fBox, bObject, link);
- DoToField("\pfBoxLocation", (Ptr) &fBoxLocation, bRect, link);
- inherited::Fields(DoToField, link);
- }
- #endif
-
-
- pascal void TBox::IBox(Rect *itsLocation)
- { fLocation = *itsLocation; }
-
- pascal void TBox::DrawShape()
- {
- PenSize(4,4);
- FrameRoundRect(&fLocation, 20,20);
- }
-
- pascal void TBox::NeedDiskSpace(long *data)
- {
- data = data + sizeof(fLocation);
- }
-
- pascal void TBox::Read(short aRefNum)
- {
- long bytes;
-
- bytes = sizeof(fLocation); // bytes to store location
- FailOSErr(FSRead(aRefNum, &bytes, (Ptr) &fLocation));
- }
-
-
- pascal void TBox::Write(short aRefNum)
- {
- long bytes;
-
- bytes = sizeof(fLocation); // bytes to store location
- FailOSErr(FSWrite(aRefNum, &bytes, (Ptr) &fLocation));
- }
-
- #ifdef qDebug
- pascal void TBox::Fields(pascal void (*DoToField) (StringPtr fieldName,
- Ptr fieldAddr, short fieldType, void *link), void *link)
- {
- DoToField("\pTBox", nil, bClass, link);
- DoToField("\pfLocation", (Ptr) &fLocation, bRect, link);
- inherited::Fields(DoToField, link);
- }
- #endif
-
-
- TTEApplication *gTEApplication;
-
- int main()
- {
- InitToolBox();
- if (ValidateConfiguration(&gConfiguration))
- {
- InitUMacApp(8);
- InitUPrinting();
- InitUTEView();
- gTEApplication = new TTEApplication;
- FailNIL(gTEApplication);
- gTEApplication->ITEApplication(kFileType);
- gTEApplication->Run();
- }
- else StdAlert(phUnsupportedConfiguration);
- return 0;
- }
-